home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Archives / ForCLI / cdtools1_0.lha / delold.c < prev    next >
C/C++ Source or Header  |  1993-10-20  |  4KB  |  173 lines

  1. #include <exec/types.h>
  2. #include <exec/memory.h>
  3. #include <exec/libraries.h>
  4. #include <dos/dosextens.h>
  5.  
  6. #include <proto/exec_protos.h>
  7. #include <proto/dos_protos.h>
  8. #include <clib/alib_protos.h>
  9.  
  10. #include <stdlib.h>
  11.  
  12. #ifndef __COMMODORE_DATE__
  13. #define __COMMODORE_DATE__ __DATE__
  14. #endif
  15.  
  16. #define BASENAME "DelOld"
  17.  
  18. const STATIC char VER[] = "$VER: " BASENAME " 1.0 (" __COMMODORE_DATE__ ")";
  19. const STATIC char Basename[] = BASENAME;
  20.  
  21. #define MAXPATHLEN 512
  22. #define POODLESIZE (1024*10)
  23.  
  24. STATIC VOID ClearMem(VOID *mem,ULONG size)
  25. {
  26. UBYTE *ptr = mem;
  27.  
  28.    while (size--)
  29.       *ptr++ = 0;
  30. }
  31.  
  32. STATIC ULONG Strlen(STRPTR s)
  33. {
  34. STRPTR b = s;
  35.  
  36.    while (*s) ++s;
  37.  
  38.    return (s-b);
  39. }
  40.  
  41. __stkargs void _main(int unused_arglen,char *unused_argptr)
  42. {
  43. int ret = RETURN_FAIL;
  44.  
  45.    if ((DOSBase->lib_Version>=37) && (SysBase->lib_Version >= 37))
  46.       {
  47.       LONG array[] = { NULL, NULL, FALSE, FALSE };
  48.       struct RDArgs *rdargs = ReadArgs("PATTERN/M/A,DAYS/N/K,QUIET/S,ALL/S", array, NULL);
  49.       if (rdargs)
  50.      {
  51.      struct DateStamp now = { 0,0,0 };
  52.      LONG days = 7;
  53.  
  54.      if (array[1])
  55.         days = *(LONG *)array[1];
  56.  
  57.      DateStamp(&now);
  58.      if (now.ds_Days >= days)
  59.         now.ds_Days -= days;
  60.      else
  61.         now.ds_Days = 0;
  62.  
  63.      struct List delfiles;
  64.      NewList(&delfiles);
  65.  
  66.      VOID *pool;
  67.  
  68.      if (pool = LibCreatePool(MEMF_ANY, POODLESIZE, POODLESIZE))
  69.         {
  70.         struct AnchorPath *anchorpath = LibAllocPooled(pool,sizeof(struct AnchorPath)+MAXPATHLEN);
  71.         if (anchorpath)
  72.            {
  73.            ClearMem(anchorpath,sizeof(struct AnchorPath)+MAXPATHLEN);
  74.            anchorpath->ap_BreakBits = SIGBREAKF_CTRL_C;
  75.            anchorpath->ap_Strlen   = MAXPATHLEN;
  76.  
  77.            STRPTR *multi = (STRPTR *)array[0];
  78.            ret = RETURN_OK;
  79.  
  80.            while (*multi) /* parse each pattern */
  81.           {
  82.           LONG mferr = MatchFirst(*multi,anchorpath);
  83.           while (mferr == 0)
  84.              {
  85.              if (anchorpath->ap_Flags &  APF_DIDDIR)
  86.             anchorpath->ap_Flags &= ~(APF_DIDDIR | APF_DODIR);
  87.              else
  88.             {
  89.             if (anchorpath->ap_Info.fib_DirEntryType > 0) /* directory */
  90.                {
  91.                if (array[3])
  92.                   anchorpath->ap_Flags |= APF_DODIR;
  93.                }
  94.             else if (anchorpath->ap_Info.fib_DirEntryType < 0) /* file */
  95.                {
  96.                if (CompareDates(&now,&anchorpath->ap_Info.fib_Date)<=0)
  97.                   {
  98.                   if (!FindName(&delfiles,anchorpath->ap_Buf)) /* no doubles */
  99.                  {
  100.                  struct Node *node = LibAllocPooled(pool,sizeof(struct Node));
  101.                  if (node)
  102.                     {
  103.                     ULONG size = Strlen(anchorpath->ap_Buf)+1;
  104.                     if (node->ln_Name = LibAllocPooled(pool,size))
  105.                        CopyMem(anchorpath->ap_Buf,node->ln_Name,size);
  106.                     else
  107.                        {
  108.                        PrintFault(ret = ERROR_NO_FREE_STORE,Basename);
  109.                        break;
  110.                        };
  111.                     AddTail(&delfiles,node);
  112.                     }
  113.                  else
  114.                     {
  115.                     PrintFault(ret = ERROR_NO_FREE_STORE,Basename);
  116.                     break;
  117.                     };
  118.                  };
  119.                   };
  120.                };
  121.             };
  122.              mferr = MatchNext(anchorpath);
  123.              };
  124.           multi++;
  125.           };
  126.  
  127.            for (struct Node *node = delfiles.lh_Head; node->ln_Succ; node = node->ln_Succ)
  128.           {
  129.           char *name = node->ln_Name;
  130.           if (name)
  131.              {
  132.              if (array[2])
  133.             DeleteFile(name);
  134.              else
  135.             {
  136.             PutStr(name);
  137.             if (DeleteFile(name))
  138.                PutStr(" Deleted\n");
  139.             else
  140.                PrintFault(ret = IoErr()," Not Deleted");
  141.             };
  142.              };
  143.           };
  144.            }
  145.         else
  146.            PrintFault(ret = ERROR_NO_FREE_STORE,Basename);
  147.  
  148.         LibDeletePool(pool);
  149.         }
  150.      else
  151.         PrintFault(ret = ERROR_NO_FREE_STORE,Basename);
  152.  
  153.      FreeArgs(rdargs);
  154.      }
  155.       else
  156.      PrintFault(ret = IoErr(),Basename);
  157.       }
  158.    else
  159.       {
  160.       if (DOSBase->lib_Version >= LIBRARY_MINIMUM)
  161.      {
  162.      if (Output())
  163.         {
  164.         Write(Output(),Basename,sizeof(Basename)-1);
  165.         Write(Output(),": Can't open dos.library v37 (OS 2.04)\n",39);
  166.         };
  167.      ret = ERROR_INVALID_RESIDENT_LIBRARY;
  168.      };
  169.       };
  170.  
  171.    _exit(ret);
  172. }
  173.